package com.hh.base.ui

import android.content.Intent
import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import com.hh.base.util.ext.getGenericsClass
import com.hh.base.util.ui.ToastUtil
import com.hh.base.widget.dialog.MultifunctionDialog
import com.hh.base.widget.pickerview.builder.TimePickerBuilder
import com.hh.base.widget.pickerview.listener.OnTimeSelectListener
import java.util.Calendar
import java.util.Date

/**
 * Create By hHui on 2025/4/2 15:25
 *
 * @description activity基类
 */
abstract class BaseActivity<T : BaseViewModel, R : ViewDataBinding> : AppCompatActivity(), BaseView {
    val TAG = this.javaClass.simpleName
    lateinit var viewModel: T
    lateinit var dataBinding: R
    private var alertDialog: MultifunctionDialog? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel = ViewModelProvider(this)[this.getGenericsClass(0)]
        dataBinding = DataBindingUtil.setContentView(this, getLayoutId())
        dataBinding.lifecycleOwner = this
        this.lifecycle.addObserver(viewModel)
        val kClass = dataBinding::class.java
        val method = kClass.getMethod("setViewModel", this.getGenericsClass(0))
        method.invoke(dataBinding, viewModel)
        initObserve()
    }

    override fun onDestroy() {
        super.onDestroy()
        this.lifecycle.removeObserver(viewModel)
    }

    @LayoutRes
    abstract fun getLayoutId(): Int


    override fun showToast(msg: String) {
        ToastUtil.showToast(this, msg)
    }

    override fun alertMessage(msg: String) {
        tryCreateDialog()
        alertDialog?.alertMessage(msg)
    }

    override fun alertTitleAndMessage(title: String, msg: String) {
        tryCreateDialog()
        alertDialog?.alertTitleAndMessage(title, msg)
    }

    override fun showLoading(msg: String) {
        tryCreateDialog()
        alertDialog?.showLoading(msg, false, "", null)
    }

    override fun showLoadingCancel(msg: String, cancelText: String, cancelListener: MultifunctionDialog.AlertCancelListener) {
        tryCreateDialog()
        alertDialog?.showLoading(msg, true, cancelText, cancelListener)
    }

    override fun alertOption(title: String, msg: String, clickListener: MultifunctionDialog.AlertClickListener, sureText: String, cancelText: String) {
        tryCreateDialog()
        alertDialog?.alertOption(title, msg, clickListener, sureText, cancelText)
    }

    override fun hideLoading() {
        alertDialog?.closeLoading()
    }

    override fun jump2Activity(cla: Class<*>, isFinishBefore: Boolean, map: Map<String, String>?) {
        val intent = Intent(this, cla)
        if (!map.isNullOrEmpty()) map.forEach { intent.putExtra(it.key, it.value) }
        startActivity(intent)
        if (isFinishBefore) finish()
    }

    override fun showDateTimerPicker(callback: OnTimeSelectListener, date: Date?, array: BooleanArray) {
        val builder = TimePickerBuilder(this, callback).setType(array)
        date?.let { a ->
            builder.setDate(Calendar.getInstance().let { it.time = a; it })
        }
        builder.build().show()
    }


    private fun tryCreateDialog() {
        if (alertDialog == null) {
            alertDialog = MultifunctionDialog(this)
        }
    }


    private fun initObserve() {
        viewModel.toastMessage.observe(this) {
            it.getContentIfNotHandled()?.let { msg ->
                showToast(msg)
            }
        }
        viewModel.alertMessage.observe(this) {
            it.getContentIfNotHandled()?.let { msg ->
                alertMessage(msg)
            }
        }
        viewModel.alertTitleAndMessage.observe(this) {
            it.getContentIfNotHandled()?.let { (title, msg) ->
                alertTitleAndMessage(title, msg)
            }
        }
        viewModel.loadingMessage.observe(this) {
            it.getContentIfNotHandled()?.let { msg ->
                showLoading(msg)
            }
        }
        viewModel.hideLoading.observe(this) {
            it.getContentIfNotHandled()?.let {
                hideLoading()
            }
        }
        viewModel.loadingCancel.observe(this) {
            it.getContentIfNotHandled()?.let { (msg, cancelText, listener) ->
                showLoadingCancel(msg, cancelText, listener)
            }
        }
        viewModel.alertOption.observe(this) {
            it.getContentIfNotHandled()?.let { (title, msg, clickListener, sureText, cancelText) ->
                alertOption(title, msg, clickListener, sureText, cancelText)
            }
        }
        viewModel.jump2Activity.observe(this) {
            it.getContentIfNotHandled()?.let { (cla, isFinishBefore, map) ->
                jump2Activity(cla, isFinishBefore, map)
            }
        }
        viewModel.showDateTimePicker.observe(this) {
            it.getContentIfNotHandled()?.let { (date, listener, array) ->
                showDateTimerPicker(listener, date, array)
            }
        }
    }
}